<template>
	<view class="content">
		<view class="informat">
			<view class="formItem">
				<view class="lab"><text>*</text>原密码</view>
				<input class="uni-input" v-model.trim="form.oldpassword" placeholder="请输入您的原密码" placeholder-style="color:#ccc"/>
			</view>
			<view class="formItem">
				<view class="lab"><text>*</text>新密码</view>
				<input class="uni-input" v-model.trim="form.newpassword" placeholder="请输入新密码至少6位" placeholder-style="color:#ccc"/>
			</view>
			<view class="formItem">
				<view class="lab"><text>*</text>确认新密码</view>
				<input class="uni-input" v-model.trim="form.re_newpassword" placeholder="请确认新密码" placeholder-style="color:#ccc"/>
			</view>
		</view>
		<button class="primary" @tap="confirmModify()">确认修改</button>
	</view>
</template>

<script>
	import common from '@/static/js/common.js';
	
	export default {
		data() {
			return {
				domain: getApp().globalData.DOMAIN,
				form: {
					oldpassword: '',
					newpassword: '',
					re_newpassword: ''
				},
				rules: [
					{name: 'oldpassword', rule: 'empty', msg: '原密码不能为空'},
					{name: 'newpassword', rule: 'empty', msg: '新密码不能为空'},
					{name: 're_newpassword', rule: 'empty', msg: '确认新密码不能为空'},
				],
			}
		},
		mounted() {
			
		},
		methods: {
			confirmModify() {
				let that = this;
				let valid = common.validator(that.rules, that.form)
				if (!valid.validator) { uni.showToast({ title: valid.msg, icon: 'none' });return false;}
				if (that.form.newpassword != that.form.re_newpassword) {
					uni.showToast({ title: '两次密码输入不一致', icon: 'none' });
					return false;
				}
				common.api({
					method: 'POST',
					url: '/mobile/user/resetpwd',
					data: {
						oldpassword: that.form.oldpassword,
						newpassword: that.form.newpassword,
					},
					success: function(res) {
						if (res.code != 0) {
							uni.showToast({ title: res.msg, icon: 'none' });
							return false;
						}
						uni.showToast({ title: '密码修改成功', icon: 'success' });
						uni.reLaunch({url:'/pages/user/index'})
					},
					error: function () {
						uni.showToast({ title: '数据请求异常', icon: 'none' });
					}
				});
			}
		}
	}
</script>

<style lang="less" scoped>
	.content {
		width: 100vw;
		min-height: 100vh;
		position: relative;
		.informat {
			background-color: #fff;
			padding: 0 20rpx;
			margin-bottom: 30rpx;
			.formItem:last-of-type {
				border-bottom: none;
			}
		}
		.primary {
			width: 80vw;
			margin: 60rpx auto 40rpx;
		}
	}
</style>
